package com.glorysoft.xztpda.ui.changeshift

import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseViewModel
import com.glorysoft.xztpda.base.Constant
import com.glorysoft.xztpda.entity.ChangeShiftEntity
import com.glorysoft.xztpda.model.LotRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: ChangeShiftViewModel
 * Author: XJH
 * Date: 2022/4/13 17:21
 * Description:
 * History:
 */
class ChangeShiftViewModel : BaseViewModel() {

    private val repository: LotRepository = LotRepository()
    private var xchangeshiftlist: List<ChangeShiftEntity> = arrayListOf()
    val stepList = arrayListOf("A0100","A0110","A0120","A0130","A0140")

    val callback: DeleteListener = object : DeleteListener {
        override fun onLotDelete(item: ChangeShiftEntity) {
            defUI.startActivity.call()
            shiftItems.remove(item)
        }

        override fun onLotDelete2(item: ChangeShiftEntity) {
            defUI.startActivity.call()
            shift2Items.remove(item)
        }

    }

    var shiftItems = ObservableArrayList<ChangeShiftEntity>()
    var shiftBinding =
        ItemBinding.of<ChangeShiftEntity>(BR.shiftItem, R.layout.item_change_shift)
            .bindExtra(BR.shiftDelete, callback)

    var shift2Items = ObservableArrayList<ChangeShiftEntity>()
    var shift2Binding =
        ItemBinding.of<ChangeShiftEntity>(BR.shift2Item, R.layout.item_change_shift2)
            .bindExtra(BR.shift2Delete, callback)


    fun getLotInfo(s: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTCHANGESHIFT")
        map.addProperty("LOTID", s)
        map.addProperty("EQUIPMENTID", "")
        launchOnlyResult({
            val lotInfo = repository.getLotInfo(map)
            if (!lotInfo.lot.state.equals("正在作业")) {
                defUI.toastEvent.value = ToastMessageBean("只有正在作业可以交班", false)
                return@launchOnlyResult
            }

            shiftItems.forEach {
                if (it.loT_ID.equals(lotInfo.lot.lotid)) {
                    defUI.toastEvent.value = ToastMessageBean("批次已添加到列表", false)
                    return@launchOnlyResult
                }

            }

//            val map1 = JsonObject()
//            map1.addProperty("ACTIONTYPE", "LOTQTY")
//            val entity = ChangeShiftEntity( lotInfo.lot.lotid,"",lotInfo.lot.stepname,"")
//            val data : ArrayList<ChangeShiftEntity> = arrayListOf()
//            data.add(entity)
//            map1.add("CHANGESHIFTLIST", JsonParser().parse(Gson().toJson(data)).asJsonArray)
//            val lotQty = repository.getLotQty(map1)


            //重新构建实体类，将接口需要字段传入
            val changeShiftEntity = ChangeShiftEntity(
                lotInfo.lot.lotid,
                lotInfo.lot.mainqty.toString(),
                lotInfo.lot.equipmentid,
                lotInfo.lot.stepname
            )
            changeShiftEntity.refeR_OWNER = Constant.CURRENT_USER
            changeShiftEntity.remaiN_QTY = lotInfo.lot.mainqty.toString()
            shiftItems.add(changeShiftEntity)
        })
    }

    fun shift() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "REFER")
        shiftItems.forEach {
            it.refeR_TIME = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
        }
        map.add(
            "CHANGESHIFTLIST",
            JsonParser().parse(Gson().toJson(shiftItems)).asJsonArray
        )

        launchOnlyResult({
            val shiftChange = repository.shiftChange(map)
            defUI.callObserve.value = CallObserveBean(0, shiftChange)
        })
    }

    interface DeleteListener {
        fun onLotDelete(item: ChangeShiftEntity)
        fun onLotDelete2(item: ChangeShiftEntity)
    }


    fun initShift(s : String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "SELECT")
        map.addProperty("LOTID", s)
        launchOnlyResult({
            val shiftChange = repository.shiftChange(map)
            if (shiftChange.xchangeshiftlist.isEmpty()){
                defUI.toastEvent.value = ToastMessageBean("返回数据为空", false)
                return@launchOnlyResult
            }
            shift2Items.forEach { item ->
                shiftChange.xchangeshiftlist.forEach {
                    if (item.loT_ID.equals(it.loT_ID)) {
                        defUI.toastEvent.value = ToastMessageBean("批次已添加到列表", false)
                        return@launchOnlyResult
                    }

                    it.tO_OWNER = Constant.CURRENT_USER
                }


            }

            shift2Items.addAll(shiftChange.xchangeshiftlist)
            defUI.callObserve.value = CallObserveBean(1, shiftChange)
        })
    }

    fun checkShiftLot(s: String) {
        val checkShiftLotList = checkShiftLotList(s)
        if (checkShiftLotList != null) {
            shift2Items.forEach {
                if (it.loT_ID == s) {
                    defUI.toastEvent.value = ToastMessageBean("当前批次已添加到列表中", false)
                    return
                }
            }
            checkShiftLotList.tO_OWNER = Constant.CURRENT_USER
            checkShiftLotList.tO_TIME = FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd")
            shift2Items.add(checkShiftLotList)
        }
    }

    private fun checkShiftLotList(s: String): ChangeShiftEntity?{
        xchangeshiftlist.forEach {
            if (it.loT_ID == s) {
                return it
            }
        }
        defUI.toastEvent.value = ToastMessageBean("当前批次不在交班列表中", false)
        return null
    }

    fun shift2() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "TO")
        shift2Items.forEach {
            it.tO_TIME = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
        }
        map.add(
            "CHANGESHIFTLIST",
            JsonParser().parse(Gson().toJson(shift2Items)).asJsonArray
        )

        launchOnlyResult({
            val shiftChange = repository.shiftChange(map)
            defUI.toastEvent.value = ToastMessageBean("操作成功",true)
            defUI.callObserve.value = CallObserveBean(0, shiftChange)
        })

    }
}